package com.itheima.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Autowired
    private OrderDao orderDao;
    @Value("${out_put_path}")
    private String outPutPath;

    @Override
    public void addSetMealAndCheckGroupIds(Integer[] checkGroupIds, Setmeal setmeal) {
        setMealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        if (checkGroupIds != null && checkGroupIds.length > 0) {
            Map<String, Object> map = new HashMap<>();
            for (Integer checkGroupId : checkGroupIds) {
                map.put("setmeal_id", setmealId);
                map.put("checkgroup_id", checkGroupId);
                setMealDao.addSetmealAndCheckGroupRelation(map);
            }
        }
//        freeMarkerGenerateHtml();
    }

    @Override
    public PageResult findByPage(QueryPageBean pageBean) {
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        String queryString = pageBean.getQueryString();
        Page<Setmeal> page = setMealDao.findByQueryString(queryString);
        long total = page.getTotal();
        List<Setmeal> result = page.getResult();
//        freeMarkerGenerateHtml();
        return new PageResult(total, result);

    }

    @Override
    public Setmeal findById(Integer id) {
        return setMealDao.findById(id);

    }

    @Override
    public List<Setmeal> findAll() {
        return setMealDao.findAll();
    }

    @Override
    public Setmeal findAllById(int id) {
        return setMealDao.findAllById(id);
    }

    @Override
    public List<Integer> findCheckGroupById(Integer id) {
        return setMealDao.findCheckGroupById(id);
    }

    @Override
    public void edit(Integer[] checkgroupIds, Setmeal setmeal) {
        setMealDao.editSetmealByid(setmeal);
        Integer setmealId = setmeal.getId();
        setMealDao.deleteSetmealRelationCheckGroupByid(setmealId);
        for (int checkgroupId : checkgroupIds) {
            Map<String, Object> map = new HashMap<>();
            map.put("setmeal_id",setmealId);
            map.put("checkgroup_id",checkgroupId);
            setMealDao.addSetmealAndCheckGroupRelation(map);
        }
    }

    @Override
    public void deleteById(Integer id) {
        Setmeal setmeal = this.findById(id);
        if (setmeal==null){
            throw new RuntimeException();
        }
        setMealDao.deleteById(id);
    }

    /**
     * 使用freeMarker生成套餐和套餐详情html静态页面
     */
    private void freeMarkerGenerateHtml(){
        List<Setmeal> setmeals = setMealDao.findAllAndDetails();
        generateSetmeal(setmeals);
        generateMobileSetmealDetailHtml(setmeals);
    }


    /**
     * 生成套餐html页面
     * @param setmeals  套餐
     */
    private void generateSetmeal(List<Setmeal> setmeals){
        Map<String,Object> map = new HashMap<>();
        map.put("setmealList",setmeals);
        generateHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }
    /**
     * 生成套餐详情html页面
     * @param setmeals  套餐
     */
    private void generateMobileSetmealDetailHtml(List<Setmeal> setmeals){
        for (Setmeal setmeal : setmeals) {
            Map<String, Object> map = new HashMap<>();
            map.put("setmeal",setmeal);
            generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_" + setmeal.getId() + ".html",map);
        }
    }
    /**
     * 通用模板生成方法
     * @param templateFile  模板文件
     * @param pageName      产生的html文件名
     * @param map           参数
     */
    private void generateHtml(String templateFile, String pageName, Map map) {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer writer = null;
        try {
            Template template = configuration.getTemplate(templateFile);
            writer = new FileWriter(outPutPath+"/"+pageName);
            template.process(map, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}